package com.lwj.controller;

import com.github.pagehelper.PageInfo;
import com.lwj.entity.Merchant;
import com.lwj.entity.Project;
import com.lwj.entity.User;
import com.lwj.model.TypeTree;
import com.lwj.model.UserProjectRes;
import com.lwj.service.ProjectService;
import com.lwj.util.DataRet;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.List;

/**项目相关
 * Created by LWJ on 2018/4/26.
 */

@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    /**
     * 项目创建
     * @param project
     * @return
     */
    @PostMapping("/create")
    public DataRet<String> createProject(HttpSession session,Project project){
        project.setStartTime(project.getStartTimeGet());
        project.setEndTime(project.getEndTimeGet());
        return projectService.create((Merchant) session.getAttribute("merchant"),project);
    }

    /**
     * 获取
     * @param session
     * @param pageNo
     * @param pageSize
     * @param searchmethod
     * @return
     */
    @PostMapping("/init")
    public PageInfo<Project> getSearchRes(HttpSession session, @RequestParam(value = "pageNo",defaultValue ="1" )int pageNo, @RequestParam(value = "pageSize",defaultValue ="1" )int pageSize, String searchmethod){
        return projectService.getProject((Merchant) session.getAttribute("merchant"),pageNo,pageSize);
    }

    /**
     * 类型树
     * @return
     */
    @GetMapping("/tree")
    public DataRet<List<TypeTree>> getTypeTree(){
        return projectService.getTypeTree();
    }

    /**
     * 根据类型获取项目列表
     * @param pageNo
     * @param pageSize
     * @param typeId
     * @return
     */
    @PostMapping("/search")
    public PageInfo<Project> getProjectByType(@RequestParam(value = "pageNo",defaultValue ="1" )int pageNo, @RequestParam(value = "pageSize",defaultValue ="1" )int pageSize,int typeId){
        return projectService.getProjectByType(pageNo,pageSize,typeId);
    }

    /**
     * 初始化用户项目信息
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/initUserProject")
    public PageInfo<UserProjectRes> getUserProject(HttpSession session,@RequestParam(value = "pageNo",defaultValue ="1" )int pageNo, @RequestParam(value = "pageSize",defaultValue ="10" )int pageSize){
        return projectService.getUserProject((User) session.getAttribute("user"),pageNo,pageSize);
    }

    /**
     * 企业验收完毕
     */
    @PostMapping("/mechantDeal")
    public DataRet<String> merchantDeal(int projectId){
        return projectService.merchantDeal(projectId);
    }

    /**
     * 用户验收完毕
     */
    @PostMapping("/userDeal")
    public DataRet<String> userDeal(int projectId){
        return projectService.userDeal(projectId);
    }

    @PostMapping("/detail")
    public DataRet<Project> getProjectDetail(int projectId){
        return projectService.getProjectDetail(projectId);
    }

    @GetMapping("/findAll")
    public PageInfo<UserProjectRes> getAllProject(@RequestParam(value = "pageNo",defaultValue ="1" )int pageNo, @RequestParam(value = "pageSize",defaultValue ="1" )int pageSize){
        return projectService.getAllProject(pageNo,pageSize);
    }

}
